package com.wolfking.demo;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 99个线程依次打印1到99
 */
@Slf4j
public class ThreadPrint99 {


    public List<String> a(){
        return null;
    }

    public String b(){
        return null;
    }

    public static void main(String[] args) throws Exception{

        Method a = ThreadPrint99.class.getDeclaredMethod("a");
        Method b = ThreadPrint99.class.getDeclaredMethod("b");
        Type genericReturnType = a.getGenericReturnType();
        Type genericReturnType1 = b.getGenericReturnType();
        System.out.println(genericReturnType);
        System.out.println(genericReturnType1);
        Object obj = new Object();
        AtomicInteger count = new AtomicInteger(1);
//        AtomicInteger c = new AtomicInteger(0);
        for (int i = 1; i < 1000; i++) {
            int finalI = i;
            new Thread(() -> {
                while (true) {
                    synchronized (obj) {
//                        c.getAndIncrement();
                        if (count.get() == finalI) {
                            System.out.println(finalI);
                            count.getAndIncrement();
                            obj.notifyAll();
                            break;
                        } else {
                            try {
                                obj.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
        }
        Thread.sleep(5000);
//        System.out.println(c.get());
    }
}
